//
//  EndProjectViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/7.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class EndProjectViewController: BidDetailViewController {

    var businessType = "60"
    
    var applyId = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        self.load(with: "Detail")
    }
    
    
    @objc override func approvalAction(button: UIButton){
        let approval = ApprovalSuggestionViewController()
        
        approval.applyId = self.applyId
        
        approval.netPath = "projectManage/mobile/sys/approvalprocess/examineApproval"
        
        approval.ifpost = false
        
        approval.businessType = "60"
        
        self.navigationController?.present(vc: approval)
    }
    
    override func configViewControllers() -> [UIViewController] {
        return [EndProjViewController(),ApprovalRecordListViewController()]
    }
    
    override func configTitles() -> Array<String> {
        return ["申请","审批"]
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return ["id":self.applyId,"businessType": self.businessType]
    }
    
    override func path(for name: String) -> String {
        return EndProjectDetail
    }

    override func method(for name: String) -> NetMethod {
        return .get
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if let data = json.contents?["data"]?.contents?["details"]?.contents {
                let purchas = self.ctls[0] as! EndProjViewController
                var contents: [Dictionary<String,String?>] = []
                let project = data["project"]?.contents ?? [:]
                contents.append(["申请项目:":project["gsProjName"]?.rawString()])
                contents.append(["项目编号:":project["gsProjNo"]?.rawString()])
                contents.append(["归属部门:": project["applyDept"]?.rawString()])
                contents.append(["项目经理:":project["projManagerName"]?.rawString()])
                if let type = project["projType"]?.rawValue as? Int {
                    switch type {
                    case 1:
                        contents.append(["项目类型:":"合同开发"])
                        break
                    case 2:
                        contents.append(["项目类型:":"自主研发"])
                        break
                    case 3:
                        contents.append(["项目类型:":"生产销售类"])
                        break
                    case 4:
                        contents.append(["项目类型:":"工程类"])
                        break
                    case 5:
                        contents.append(["项目类型:":"运维服务类"])
                        break
                    case 6:
                        contents.append(["项目类型:":"咨询类"])
                        break
                    case 7:
                        contents.append(["项目类型:":"融资性租赁"])
                        break
                    case 8:
                        contents.append(["项目类型:":"经营性租赁"])
                        break
                    case 9:
                        contents.append(["项目类型:":"综合管理类"])
                        break
                    case 10:
                        contents.append(["项目类型:":"其他类"])
                        break
                    case 11:
                        contents.append(["项目类型:":"实施类"])
                        break
                    default:
                        contents.append(["项目类型:":"其他"])
                        break
                    }
                } else  {
                    contents.append(["项目类型:":"其他"])
                }
                contents.append(["业务产品类别:":project["productType"]?.rawString()])
                contents.append(["计划开始日期:":project["beginDate"]?.rawString()])
                contents.append(["计划结束日期:":project["endDate"]?.rawString()])
                contents.append(["实际开始日期:":data["actualStartDate"]?.rawString()])
                contents.append(["实际结束日期:":data["actualEndDate"]?.rawString()])
                contents.append(["项目计划工作量:":project["estimatedWork"]?.rawString()])
                contents.append(["项目实际工作量:":data["workload"]?.rawString()])
                let  projEstimatedRevenue = project["projEstimatedRevenue"]?.int ?? 0
                contents.append(["项目预算收入(元):": String(projEstimatedRevenue / 100).rmbformat])
                let actualIncome = data["actualIncome"]?.int ?? 0 //项目预算收入参数未知
                contents.append(["项目执行收入(元):":String(actualIncome / 100).rmbformat])
                let projBudget = project["projBudget"]?.int ?? 0
                contents.append(["项目预算成本(元):": String(projBudget / 100).rmbformat])
                let actualCost = data["actualCost"]?.int ?? 0//项目执行预算成本
                contents.append(["项目执行成本(元):":String(actualCost / 100).rmbformat])
                if projEstimatedRevenue == 0 {
                    contents.append(["项目预算毛利率::":""])
                } else {
                    let ratio = Double(projEstimatedRevenue - projBudget) / Double(projEstimatedRevenue) * 100
                    contents.append(["项目预算毛利率::":String.init(format: "%.2f", ratio) + "%"])
                }
                contents.append(["项目总体完成情况:":data["performance"]?.rawString()])
                contents.append(["项目交付成果:":data["deliverable"]?.rawString()])
                contents.append(["项目验收情况:":data["checkCase"]?.rawString()])
                contents.append(["需要继续跟踪的问题:":data["tailAfterIssue"]?.rawString()])
                
                if let ysbgs = data["checkFiles"]?.array {
                    //验收报告
                    purchas.ysbgs = ysbgs
                }
                if let xmcgs = data["attachFiles"]?.array {
                    //项目成果
                    purchas.documents = xmcgs
                }
                
                if let others = data["otherFiles"]?.array {
                    purchas.others = others
                }
                
                purchas.source = contents
                purchas.tableView.reloadData()
                if let approvalList = data["totalStep"]?.array {
                    let approval = self.ctls[1] as! ApprovalRecordListViewController
                    approval.source = approvalList
                    approval.tableView.reloadData()
                }
            }
        }
        return suc
    }
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
